﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using yzrilyzr.FAQ.client;
using yzrilyzr.FAQ.constants;
using yzrilyzr.FAQ.protocol;
using yzrilyzr.FAQ.utils;
using StringBuilder = System.Text.StringBuilder;

namespace FAQ_UI.window
{
	/// <summary>
	/// UserProfile.xaml 的交互逻辑
	/// </summary>
	public partial class UserProfile : Window, MsgPackReceiver, INotifyPropertyChanged
	{
		private FAQID faqid;
		public Drawable FAQAvatar { get; set; }
		public Drawable FAQBackground { get; set; }
		public string FAQIDInt { get; set; }
		public string FAQIDStr { get; set; }

		public string FAQLevel { get; set; }
		public string FAQLike { get; set; }

		public event PropertyChangedEventHandler PropertyChanged;

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		public UserProfile(FAQID faqid)
		{
			InitializeComponent();
			DataContext = this;
			FAQUtil.client.addReceiver(this);
			FAQUtil.client.getUserProfile(faqid, D.NAMES, D.DESCRIPTIONS, D.HEADS, D.BACKGROUNDS, D.USER_LEVELS, D.USERLIKES, K.STAIN);
			this.faqid = faqid;
			showFAQName.Text = FAQClient.getName(faqid);
			showFAQCommentName.Text = FAQUtil.client.getCommentName(faqid);
			showFAQDescription.Text = FAQUtil.getDescription(faqid);
			this.FAQAvatar = FAQUtil.getAvatar(faqid);
			this.FAQBackground = FAQUtil.getBackground(faqid);
			this.FAQIDStr = faqid.toString() + "";
			this.FAQIDInt = faqid.toInt() + "";
			if (!faqid.equals(FAQUtil.client.getFAQID()))
			{
				showFAQNameButton.Visibility = Visibility.Collapsed;
				showFAQDescriptionButton.Visibility = Visibility.Collapsed;
			}
			if (FAQUtil.client.isMyFriend(faqid))
			{
				AddFriend.Visibility = Visibility.Collapsed;
				SendMsg.Visibility = Visibility.Collapsed;
			}
			else
			{
				AddFriend.Visibility = Visibility.Visible;
				SendMsg.Visibility = Visibility.Visible;
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
			FAQUtil.client.removeReceiver(this);
		}

		public void receive(MsgPackTransfer c, MsgPack p)
		{
			if (p.getCmd() == C.USER_CACHE)
			{
				Dispatcher.Invoke(new Action(() =>
				{
					this.FAQAvatar = FAQUtil.getAvatar(faqid);
					this.FAQBackground = FAQUtil.getBackground(faqid);
					long lv = p.getLong(D.USER_LEVELS);
					if (lv != 0) FAQLevel = getLVText(lv);
					long li = p.getLong(D.USERLIKES);
					if (li != 0) FAQLike = li + "";
					OnPropertyChanged(nameof(FAQAvatar));
					OnPropertyChanged(nameof(FAQBackground));
					OnPropertyChanged(nameof(FAQLevel));
					OnPropertyChanged(nameof(FAQLike));
				}));
			}
		}

		public static string getLVText(long lv)
		{
			StringBuilder ab = new System.Text.StringBuilder();
			int yzr = 500000000, bamboo = 100000000, crown = 20000000, funny = 5000000, sun = 1000000, moon = 200000, star = 40000;
			while (lv > 0)
			{
				if (lv > yzr)
				{
					lv -= yzr;
					ab.Append("\ue018");
				}
				else if (lv > bamboo)
				{
					lv -= bamboo;
					ab.Append("\ue017");
				}
				else if (lv > crown)
				{
					lv -= crown;
					ab.Append("\ue016");
				}
				else if (lv > funny)
				{
					lv -= funny;
					ab.Append("\ue015");
				}
				else if (lv > sun)
				{
					lv -= sun;
					ab.Append("\ue014");
				}
				else if (lv > moon)
				{
					lv -= moon;
					ab.Append("\ue013");
				}
				else if (lv > star)
				{
					lv -= star;
					ab.Append("\ue012");
				}
				else break;
			}
			return ab.ToString();
		}

		private void userlike(object sender, MouseButtonEventArgs e)
		{
			if (!faqid.equals(FAQUtil.client.getFAQID()))
				FAQUtil.client.asyncSend((MsgPack)new BinMsgPack(C.USER_CACHE).setTo(faqid).put(D.USERLIKES, true));
		}

		private void addFriend(object sender, MouseButtonEventArgs e)
		{
			FAQUtil.client.addFriend(faqid);
			FAQUtil.client.sendMyE2EEKey(faqid);
			FAQUtil.client.saveMyData();
			MessageBox.Show(string.Format("已向 {0} 发送申请", FAQUtil.getCommNameOrName(faqid)), "添加好友", MessageBoxButton.OK);
		}

		private void sendMsg(object sender, MouseButtonEventArgs e)
		{
			FAQUtil.eventBus.asyncSend(new BinMsgPack(App.OPEN_CHAT).setTo(faqid));
			Close();
		}

		private void editCommentName(object sender, MouseButtonEventArgs e)
		{
			if (editFAQCommentName.Visibility == Visibility.Visible)
			{
				showFAQCommentName.Text = editFAQCommentName.Text;
				FAQUtil.client.setCommentName(faqid, editFAQCommentName.Text);
				((TextBlock)sender).Text = "\ue3c9";
				FAQUtil.client.saveMyData();
				editFAQCommentName.Visibility = Visibility.Collapsed;
				showFAQCommentName.Visibility = Visibility.Visible;
			}
			else
			{
				editFAQCommentName.Visibility = Visibility.Visible;
				showFAQCommentName.Visibility = Visibility.Collapsed;
				editFAQCommentName.Text = FAQUtil.client.getCommentName(faqid);
				((TextBlock)sender).Text = "\ue5ca";
			}
		}

		private void editDescription(object sender, MouseButtonEventArgs e)
		{
			if (editFAQDescription.Visibility == Visibility.Visible)
			{
				showFAQDescription.Text = editFAQDescription.Text;
				FAQUtil.client.setDescription(editFAQDescription.Text);
				((TextBlock)sender).Text = "\ue3c9";
				FAQUtil.client.saveMyData();
				editFAQDescription.Visibility = Visibility.Collapsed;
				showFAQDescription.Visibility = Visibility.Visible;
			}
			else
			{
				editFAQDescription.Visibility = Visibility.Visible;
				showFAQDescription.Visibility = Visibility.Collapsed;
				editFAQDescription.Text = FAQUtil.getDescription(faqid);
				((TextBlock)sender).Text = "\ue5ca";
			}
		}

		private void editName(object sender, MouseButtonEventArgs e)
		{
			if (editFAQName.Visibility == Visibility.Visible)
			{
				showFAQName.Text = editFAQName.Text;
				FAQUtil.client.setName(editFAQName.Text);
				((TextBlock)sender).Text = "\ue3c9";
				FAQUtil.client.saveMyData();
				editFAQName.Visibility = Visibility.Collapsed;
				showFAQName.Visibility = Visibility.Visible;
			}
			else
			{
				editFAQName.Visibility = Visibility.Visible;
				showFAQName.Visibility = Visibility.Collapsed;
				editFAQName.Text = FAQClient.getName(faqid);
				((TextBlock)sender).Text = "\ue5ca";
			}
		}

		private void setBack(object sender, MouseButtonEventArgs e)
		{
			if (FAQUtil.client.getFAQID().equals(faqid))
			{
				ImageCropWindow w = new ImageCropWindow(1080.0 / 600.0);
				w.ShowDialog();
				CroppedBitmap b = w.getResult();
				byte[] arr = FAQUtil.ToByteArray(new PngBitmapEncoder(), b);
				FAQUtil.client.setBackground(arr);
				FAQUtil.imageCache.remove("Background" + faqid);
			}
		}

		private void setAvatar(object sender, MouseButtonEventArgs e)
		{
			if (FAQUtil.client.getFAQID().equals(faqid))
			{
				ImageCropWindow w = new ImageCropWindow(1);
				w.ShowDialog();
				CroppedBitmap b = w.getResult();
				byte[] arr = FAQUtil.ToByteArray(new PngBitmapEncoder(), b);
				FAQUtil.client.setHead(arr);
				FAQUtil.imageCache.remove("Avatar" + faqid);
			}
		}
	}
}